package com.gblfy.minio.controller;

import com.gblfy.minio.utils.JsonData;
import com.gblfy.minio.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "文件管理模块")
@RestController
@RequestMapping("/api/file/v1")
@Slf4j
public class MinioController {

    @Autowired
    private MinioUtil minioUtil;

    @ApiOperation(value = "查看存储bucket是否存在")
    @GetMapping("/bucketExists")
    public JsonData bucketExists(String bucketName) {
        return JsonData.buildSuccess(minioUtil.bucketExists(bucketName));
    }

    @ApiOperation(value = "创建存储bucket")
    @GetMapping("/makeBucket")
    public JsonData makeBucket(String bucketName) {
        minioUtil.makeBucket(bucketName);
        return JsonData.buildSuccess();
    }

    @ApiOperation(value = "删除存储bucket")
    @GetMapping("/removeBucket")
    public JsonData removeBucket(String bucketName) {
        minioUtil.removeBucket(bucketName);
        return JsonData.buildSuccess();
    }

    @ApiOperation(value = "获取全部bucket")
    @GetMapping("/getAllBuckets")
    public JsonData getAllBuckets() {
        return JsonData.buildSuccess(minioUtil.getAllBuckets());
    }



    @ApiOperation(value = "文件上传")
    @GetMapping("/upload")
    public JsonData upload(MultipartFile file) {
        minioUtil.upload(file);
        return JsonData.buildSuccess();
    }

    @ApiOperation(value = "图片预览")
    @GetMapping("/preview")
    public JsonData preview(String fileName) {
        return JsonData.buildSuccess(minioUtil.preview(fileName));
    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public JsonData download(String fileName, HttpServletResponse res) {
        minioUtil.download(fileName,res);
        return JsonData.buildSuccess();
    }

    @ApiOperation(value = "文件删除")
    @GetMapping("/remove")
    public JsonData remove(String fileName) {
        minioUtil.remove(fileName);
        return JsonData.buildSuccess();
    }
}